//获取应用实例
const app = getApp()

Component({
    data: {
      showTextAndTitleWithInput:false,
      boxes:[],
      boxId:"",
      boxCode:"",
      colors:["#52a4f0","#95cb95","#de7e56"],
      showName:""
    },

    lifetimes: {
      attached: function(options) {
        // 在组件实例进入页面节点树时执行
        wx.request({
          url: app.globalData.url+'/wechat/student_report_boxes',
          data: {
            username: app.globalData.localuserinfo.username,
          },
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          success: res2 => {
            if(res2.statusCode==200){
              this.setData({boxes:res2.data||[],showName:app.globalData.localuserinfo.showName})
            }
          },
          fail:()=>{
            console.log('fail')
          }
        })
      }
    },

    onLoad: function() {
        // var that = this;
        // wx.request({
        //   url: app.globalData.url+'/wechat/student_report_boxes',
        //   data: {
        //     username: app.globalData.localuserinfo.username,
        //   },
        //   method: 'POST',
        //   header: {
        //     'content-type': 'application/json'
        //   },
        //   success: res2 => {
        //     if(res2.statusCode==200){
        //       this.setData({boxes:res2.data||[]})
        //     }
        //   },
        //   fail:()=>{
        //     console.log('fail')
        //   }
        // })
    },



    // cellClick:function(e){
    //   const boxId=e.target.id
    //   this.setData({boxId})
    //   wx.navigateTo({
    //     url: "../student_m_report_box/student_m_report_box?boxId="+boxId,
    // });
    // },

    methods:{
      cellClick:function(e){
        const boxId=e.target.id
        this.setData({boxId})
        wx.navigateTo({
          url: "../student_m_report_box/student_m_report_box?boxId="+boxId,
      });
      },
      joinReportBox: function(){
        var that = this;
        if (this.data.boxCode == "") {
          wx.showToast({
            title: '请输入课程代码',
            icon: 'none',
            duration: 2000
          })
        }else{
          wx.request({
            url: app.globalData.url+'/wechat/join_report_box',
            data: {
              username: app.globalData.localuserinfo.username,
              boxCode: that.data.boxCode,
              token:app.globalData.token
            },
            method: 'POST',
            header: {
              'content-type': 'application/json'
            },
            success: res2 => {
              if(res2.data.status&&res2.data.status==="fail"){
                console.log(res2.data)
                wx.showToast({
                  title: res2.data.err,
                  icon: 'none',
                  duration: 2000
                })
               
              }else{
                const boxes=that.data.boxes;
               boxes.push(res2.data);
                this.setData({showTextAndTitleWithInput:false,
                  boxes})
              }
            }
          })
        }
      },
      
    showDialog: function(){
      this.setData({showTextAndTitleWithInput:true})
    },
    closeDialog: function(){
      this.setData({showTextAndTitleWithInput:false})
    },

    joinReportBox: function(){
      var that = this;
      if (this.data.boxCode == "") {
        wx.showToast({
          title: '请输入课程代码',
          icon: 'none',
          duration: 2000
        })
      }else{
        wx.request({
          url: app.globalData.url+'/wechat/join_report_box',
          data: {
            username: app.globalData.localuserinfo.username,
            boxCode: that.data.boxCode,
            token:app.globalData.token
          },
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          success: res2 => {
            if(res2.data.status&&res2.data.status==="fail"){
              console.log(res2.data)
              wx.showToast({
                title: res2.data.err,
                icon: 'none',
                duration: 2000
              })
             
            }else{
              const boxes=that.data.boxes;
             boxes.push(res2.data);
              this.setData({showTextAndTitleWithInput:false,
                boxes})
            }
          }
        })
      }
    },

    cellClick:function(e){
      const boxId=e.target.id
      this.setData({boxId})
      wx.navigateTo({
        url: "../student_m_report_box/student_m_report_box?boxId="+boxId,
    });
    },
    changeBoxCode:function(e){
      this.setData({
        boxCode: e.detail.value
      })
    }
    },

    
  
})
